package org.apache.dolphinscheduler.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.dolphinscheduler.api.dto.UserDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * @ClassName : UserApiService
 * @Description :
 * @Author : zzj
 * @Date: 2021-06-10 11:34
 */
@Service
public class UserApiService {
    @Value("${dpl.url}")
    private String dplUrl;
    public  UserDto getUsers(String userName){
        UserDto userDto=null;
        try {
            RestTemplate client = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            //  请勿轻易改变此提交方式，大部分的情况下，提交方式都是表单提交
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //  封装参数，千万不要替换为Map与HashMap，否则参数无法传递
            //HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
            //  执行HTTP请求
            ResponseEntity<String> response = client.getForEntity(dplUrl+"/user/getUsers?deptId=&userName="+userName+"&currentPage=0&pageSize=10", String.class );
            JSONObject object= JSONObject.parseObject(response.getBody());
            String listStr=object.getJSONObject("DS").get("list").toString();
            System.out.println(listStr);
            List<UserDto> list= JSON.parseArray(listStr,UserDto.class);

            for(int i=0;i<list.size();i++){
                if(list.get(i).getLoginName().equals(userName)){
                    userDto=list.get(i);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userDto;

    }
    public  boolean  login(String username, String password,StringBuilder message){
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        //  请勿轻易改变此提交方式，大部分的情况下，提交方式都是表单提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //  封装参数，千万不要替换为Map与HashMap，否则参数无法传递
        MultiValueMap<String, String> params= new LinkedMultiValueMap<String, String>();
        //  也支持中文
        params.add("password", Base64.getEncoder().encodeToString(password.getBytes()));
        params.add("loginName", username);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
        //  执行HTTP请求
        try {
            ResponseEntity<String> response = client.exchange(dplUrl+"/login", HttpMethod.POST, requestEntity , String.class );
            JSONObject object= JSONObject.parseObject(response.getBody());
            String code=object.getJSONObject("DS").getString("code");
            message.append(object.getJSONObject("DS").getString("message"));
            if(code.equals("2000")){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}

